package org.iwidget.desktop.core;
import java.util.Hashtable;

/**
 *
 * @author Muhammad Hakim A
 */

public class IwidgetRepositoryCacheHandler extends Hashtable
{
    public class Cache
    {

        public final int getCacheCount()
        {
            return hash.size();
        }

        public final int getCacheSize()
        {
            return cacheSize;
        }

        public final int getMaximumCacheSize()
        {
            return maximumCacheSize;
        }

        public final void setMaximumCacheSize(int size)
        {
            maximumCacheSize = size;
        }

        public synchronized Object getCacheItem(String hashName)
        {
            return hash.get(hashName);
        }

        public synchronized void insertCacheItem(String hashName, byte data[])
        {
            hash.put(hashName, data);
        }

        public synchronized void insertCacheItem(String hashName, String data)
        {
            insertCacheItem(hashName, data.getBytes());
        }

        public synchronized void removeCacheItem(String hashName)
        {
            hash.remove(hashName);
        }

        public synchronized void clear()
        {
            cacheSize = 0;
            hash = new Hashtable();
        }

        private Hashtable hash;
        private int cacheSize;
        private int maximumCacheSize;

        public Cache()
        {
            hash = new Hashtable(10);
            cacheSize = 0;
            maximumCacheSize = 32768;
        }
    }


    public IwidgetRepositoryCacheHandler()
    {
    }

    public Cache getCache(String cacheName)
    {
        return (Cache)get(cacheName);
    }

    public Cache createCache(String cachName)
    {
        Cache cache = new Cache();
        put(cachName, cache);
        return cache;
    }

    private static final long serialVersionUID = 0x3339373730313733L;
}